package com.zzt.service;

import com.zzt.dao.GoodsinfoMapper;
import com.zzt.dao.GoodstypeMapper;
import com.zzt.dao.GwcMapper;
import com.zzt.entity.*;
import com.zzt.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsinfoServiceImpl implements GoodsinfoService{
    @Resource
    GoodstypeMapper goodstypeMapper;
    @Resource
    GoodsinfoMapper goodsinfoMapper;
    @Resource
    GwcMapper gwcMapper;
//    @Resource
//    RedisUtils redisUtils;
//
//    private static int ExpireTime = 600;   // redis中存储的过期时间600s
    //查找全部商品
    @Override
    public List<Goodsinfo> queryAllGoods() {
        //因为涉及到了分页 不能用redis缓存了
//        try {
//            boolean goodsCache = redisUtils.hasKey("goodsCache");   //是否存在缓存
//            if(goodsCache){ //存在
//                System.out.println("直接在缓存中取商品");
//                return (List<Goodsinfo>)redisUtils.get("goodsCache");
//            }
//        } catch (Exception exception) {
//            System.out.println("无缓存");
//        }
        //若不存在 从数据库查找
        List<Goodsinfo> goodsinfos = goodsinfoMapper.selectByExample(null);
        //找到后存入缓存
//        redisUtils.set("goodsCache", goodsinfos, ExpireTime);
        return goodsinfos;
    }


    @Override
    public Goodsinfo queryGoods(int gno) {
        Goodsinfo goodsinfo = goodsinfoMapper.selectByPrimaryKey(gno);
        Goodstype goodstype = goodstypeMapper.selectByPrimaryKey(goodsinfo.getTno());
        goodsinfo.setGoodstype(goodstype);
        return goodsinfo;
    }

    //添加商品到购物车
    @Override
    public Gwc addGoods(int mno, int gno) {
        Gwc gwc;
        //查一下购物车中有没有这个人这个商品
        gwc = gwcMapper.selectByMnoAndGno(mno, gno);
        if (gwc == null) {  //如果还没添加进去
            //查一下商品详情
            Goodsinfo goodsinfo = goodsinfoMapper.selectByPrimaryKey(gno);
            //把商品情况和用户添加到GWC表中(ps: 数量待会+1)
            gwc = new Gwc(mno, goodsinfo.getGno(), goodsinfo.getGname(),
                    goodsinfo.getPic(), goodsinfo.getPrice(), 1);
            gwcMapper.insert(gwc);
        } else {  //如果已经加入了  修改数量+1
            gwc.setNum(gwc.getNum() + 1);
            gwcMapper.updateByPrimaryKey(gwc);
        }
        return gwc;
    }

    @Override
    public List<Goodsinfo> queryInputs(String gname,int tno) {
        GoodsinfoExample goodsinfoExample = new GoodsinfoExample();
        GoodsinfoExample.Criteria criteria = goodsinfoExample.createCriteria();
        if(tno != -1){  //如果同时分类查询了
            criteria.andTnoEqualTo(tno);    // 同时符合2种情况
        }
        // 如果没有进行分类： 那就只需要查搜索的内容
        criteria.andGnameLike("%"+gname+"%");
//    criteria.price
        List<Goodsinfo> goodsinfos = goodsinfoMapper.selectByExample(goodsinfoExample);
        return goodsinfos;
    }

    @Override
    public List<Goodsinfo> queryType(int tno) {
        GoodsinfoExample goodsinfoExample = new GoodsinfoExample();
        GoodsinfoExample.Criteria criteria = goodsinfoExample.createCriteria();
        criteria.andTnoEqualTo(tno);
        List<Goodsinfo> goodsinfos = goodsinfoMapper.selectByExample(goodsinfoExample);
        return goodsinfos;
    }

    @Override
    public List<Goodsinfo> queryPrice(int sp, int ep) {
        GoodsinfoExample goodsinfoExample = new GoodsinfoExample();
        GoodsinfoExample.Criteria criteria = goodsinfoExample.createCriteria();
        criteria.andPriceBetween((float)sp, (float)ep);
        List<Goodsinfo> goodsinfos = goodsinfoMapper.selectByExample(goodsinfoExample);
        return goodsinfos;
    }

}
